package service.calculator.crystalite.laguna;

import java.math.BigDecimal;

import models.Payment;
import models.Rate;
import service.calculator.crystalite.CrystaliteBillingCalculatorImpl;
import service.calculator.crystalite.CrystalitePaymentType;
import util.Rounder;

public class CrystaliteLagunaBillingCalculatorImpl extends
		CrystaliteBillingCalculatorImpl {

	public BigDecimal calculatePayment(Rate rate, Payment payment) {
		BigDecimal paymentCalculation = null;
		CrystalitePaymentType paymentType = CrystalitePaymentType
				.valueOf(payment.paymentType);
		final BigDecimal paymentHrs = BigDecimal.valueOf(payment.hours);
		final BigDecimal rateValue = rate.value;

		// 12.50 per day
		final BigDecimal ecolaRate = BigDecimal.valueOf(1.5625);
		
		// 13.00 per day
		final BigDecimal seaRate = BigDecimal.valueOf(1.625);

		switch (paymentType) {
		case BASIC:
			BigDecimal basicBillingRate = calculateBasicBillingRate(rateValue);
			paymentCalculation = basicBillingRate.multiply(paymentHrs);
			paymentCalculation = Rounder.roundup(paymentCalculation);
			break;
		case ECOLA:
		case CTPA:
			paymentCalculation = ecolaRate.multiply(paymentHrs);
			paymentCalculation = Rounder.roundup(paymentCalculation);
			break;
		case SEA:
			paymentCalculation = seaRate.multiply(paymentHrs);
			paymentCalculation = Rounder.roundup(paymentCalculation);
			break;
		default:
			paymentCalculation = super.calculatePayment(rate, payment);
		}

		return paymentCalculation;
	}

	private BigDecimal calculateBasicBillingRate(BigDecimal rateValue) {
		BigDecimal basicRate = BigDecimal.valueOf(0.00);
		BigDecimal governmentBenefits = computeGovernmentBenefitsPerHour();

		basicRate = basicRate.add(rateValue);
		basicRate = basicRate.add(governmentBenefits);

		return basicRate;
	}

	protected BigDecimal computeGovernmentBenefitsPerHour() {
		BigDecimal thirteenthMonthPayPerDay = BigDecimal.valueOf(26.56);
		BigDecimal sssPerDay = BigDecimal.valueOf(22.40);
		BigDecimal philhealthPerDay = BigDecimal.valueOf(3.97);
		BigDecimal pagIbigPerDay = BigDecimal.valueOf(3.85);
		BigDecimal ecMcPerDay = BigDecimal.valueOf(0.38);

		BigDecimal governmentBenefits = BigDecimal.valueOf(0.00);
		governmentBenefits = governmentBenefits.add(thirteenthMonthPayPerDay);
		governmentBenefits = governmentBenefits.add(sssPerDay);
		governmentBenefits = governmentBenefits.add(philhealthPerDay);
		governmentBenefits = governmentBenefits.add(pagIbigPerDay);
		governmentBenefits = governmentBenefits.add(ecMcPerDay);

		governmentBenefits = governmentBenefits.divide(BigDecimal.valueOf(8)); // Compute per hour

		return governmentBenefits;
	}

}
